package com.zygh.policeasset.controller;

import cn.hutool.core.util.StrUtil;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.mapper.UnitInformationMapper;
import com.zygh.policeasset.pojo.UnitInformation;
import com.zygh.policeasset.service.UnitInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/unitInformation"})
@Api(tags = {"设计咨询监理及其他模块"})
@RestController
public class UnitInformationController {
    @Autowired
    UnitInformationService unitInformationService;
    @Autowired
    UnitInformationMapper unitInformationMapper;

    @RequestMapping(value = {"/"}, method = {RequestMethod.GET})
    @ApiOperation("获取项目设计咨询监理及其他列表")
    public Object findByProject(@RequestParam(value = "projectNo", required = false) String projectNo,
                                @RequestParam(value = "name", required = false) String name,
                                @RequestParam(value = "status", required = false) Integer status,
                                @RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                                @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
                                @RequestParam(value = "unitNature", required = false) String unitNature,
                                @RequestHeader(value = "findAll", required = false) boolean findAll,
                                @RequestHeader(value = "userId", required = false) Integer userId) {
        if (findAll) {
            return this.unitInformationService.findByProject(projectNo, name, status, pageNum, pageSize, unitNature, null);
        }
        return this.unitInformationService.findByProject(projectNo, name, status, pageNum, pageSize, unitNature, userId);
    }

    @RequestMapping(value = {"/getadd"}, method = {RequestMethod.GET})
    @ApiOperation("获取可添加项目")
    public Object getAdd() {
        return this.unitInformationService.getAdd();
    }

    @RequestMapping(value = {"/add"}, method = {RequestMethod.POST})
    @ApiOperation("添加")
    public Object add(@RequestBody UnitInformation params) {
        if (StrUtil.isEmpty(params.getProjectNo())) {
            return new CommonResult().validateFailed("项目编号不能为空");
        }
        if (this.unitInformationMapper.getProjectNoCount(params.getProjectNo()).intValue() > 0) {
            return new CommonResult().validateFailed("项目重复添加");
        }
        if (this.unitInformationService.add(params).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("添加错误");
    }

    @RequestMapping(value = {"/update"}, method = {RequestMethod.POST})
    @ApiOperation("修改")
    public Object update(@RequestBody UnitInformation params) {
        if (StrUtil.isEmpty(String.valueOf(params.getId()))) {
            return new CommonResult().validateFailed("ID不能为空");
        }
        if (this.unitInformationService.update(params).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改错误");
    }

    @RequestMapping(value = {"/remove"}, method = {RequestMethod.GET})
    @ApiOperation("删除")
    public Object remove(@RequestParam int id) {
        if (StrUtil.isEmpty(String.valueOf(id))) {
            return new CommonResult().validateFailed("id不能为空");
        }
        if (this.unitInformationService.remove(id).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除错误");
    }

    @RequestMapping(value = {"/submit"}, method = {RequestMethod.GET})
    @ApiOperation("提交接口")
    public Object submit(@RequestParam int id, @RequestParam int status) {
        if (StrUtil.isEmpty(String.valueOf(id))) {
            return new CommonResult().validateFailed("id不能为空");
        }
        if (this.unitInformationService.updateByid(Integer.valueOf(id), Integer.valueOf(status)).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("提交失败");
    }

    @RequestMapping(value = {"/findById"}, method = {RequestMethod.GET})
    @ApiOperation("通过id查询")
    public Object findById(@RequestParam Integer id) {
        return new CommonResult().success(this.unitInformationService.findById(id));
    }
}